import 'package:flutter/material.dart';

class Calendar extends StatefulWidget {
  const Calendar({Key? key}) : super(key: key);

  @override
  _CalendarState createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  late List _dates;

  // List<Widget> widgetList = <Widget>[];
  @override
  void initState() {
    super.initState();
    int year = 2022;
    List list = [];
    for (var i = 0; i < 24; i++) {
      year = 2022 + (i / 12).floor();
      var month = i % 12 + 1;
      var days = this.getDaysInMonth(month, year);
      var strMonth = month < 10 ? '0' + month.toString() : month.toString();

      var dateString = year.toString() + '-' + strMonth + '-01';
      var week = DateTime.parse(dateString).weekday;

      List<dynamic> day = [];
      for (var j = 0; j < week; j++) {
        day.add({'day': ''});
      }

      for (var j = 0; j < days; j++) {
        var dayObj = new Map();
        dayObj['day'] = j + 1;
        if (year == 2022 && month == 1 && j == 3) {
          dayObj['begin'] = true;
        }
        if (year == 2022 && month == 2 && j == 27) {
          dayObj['single'] = true;
        }
        if (year == 2022 &&
            month == 1 &&
            (j == 4 || j == 5 || j == 6 || j == 7 || j == 8||j == 9 || j == 10 || j == 11 || j == 12 || j == 13)) {
          dayObj['middle'] = true;
        }
        if (year == 2022 && month == 1 && j == 14) {
          dayObj['end'] = true;
        }

        day.add(dayObj);
      }

      var obj = {"year": year, "month": month, "days": day};
      list.add(obj);
    }

    print(list[0].toString());

    setState(() {
      _dates = list;
    });
  }

  getDaysInMonth(month, year) {
    // month = month + '';

    var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (month == '02' || month == 2) {
      daysInMonth[1] =
          (((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400))
              ? 29
              : 28;
    }
    var flag = month.toString().substring(0, 1);
    if (flag == 0) {
      month = month.toString().substring(1, 1);
    }
    return daysInMonth[month - 1];
  }

  @override
  Widget build(BuildContext context) {
    var screenW = MediaQuery.of(context).size.width;
    print(screenW.toString());
    return Scaffold(
        appBar: AppBar(
          title: Text('Calendar22'),
          backgroundColor: Color(0xff123456),
          elevation: 0,
        ),
        body: Container(
          alignment: Alignment.topLeft,
          color: Colors.black,
          child: Flex(
            direction: Axis.vertical,
            children: [
              Container(
                padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                child: Flex(
                  direction: Axis.horizontal,
                  children: [
                    Expanded(
                      child: Text(
                        '日',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '一',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '二',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '三',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '四',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '五',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Text(
                        '六',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white),
                      ),
                      flex: 1,
                    ),
                  ],
                ),
              ),
              Expanded(
                child: SingleChildScrollView(
                  physics: BouncingScrollPhysics(),
                  child: Flex(
                    direction: Axis.vertical,
                    children: List<Widget>.generate(_dates.length, (index) {
                      return Flex(
                        direction: Axis.vertical,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.fromLTRB(20, 10, 10, 0),
                            child: Text(
                              '${_dates[index]['year']}年${_dates[index]['month']}月',
                              style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xffffffff)),
                            ),
                          ),
                          Wrap(
                            direction: Axis.horizontal,
                            alignment: WrapAlignment.start,
                            spacing: 0,
                            runSpacing: 0,
                            children: List<Widget>.generate(
                                _dates[index]['days'].length, (i) {
                              if (_dates[index]['days'][i]['single'] == true ||
                                  _dates[index]['days'][i]['begin'] == true ||
                                  _dates[index]['days'][i]['middle'] == true ||
                                  _dates[index]['days'][i]['end'] == true) {
                                Radius left = Radius.circular(0);
                                Radius right = Radius.circular(0);
                                if (_dates[index]['days'][i]['single'] ==
                                        true ||
                                    _dates[index]['days'][i]['begin'] == true) {
                                  left = Radius.circular(screenW / 7.0 / 2.0);
                                }
                                if (_dates[index]['days'][i]['single'] ==
                                        true ||
                                    _dates[index]['days'][i]['end'] == true) {
                                  right = Radius.circular(screenW / 7.0 / 2.0);
                                }

                                return ClipRRect(
                                  borderRadius: BorderRadius.horizontal(
                                      left: left, right: right),
                                  child: Container(
                                      color: Colors.orange,
                                      width: (screenW / 7.0-0.5).ceil().toDouble(),
                                      height: (screenW / 7.0).ceil().toDouble(),
                                      alignment: Alignment.center,
                                      child: Text(
                                        '${_dates[index]['days'][i]['day'].toString()}',
                                        style: TextStyle(color: Colors.white),
                                      )),
                                );
                              }
                              return Container(
                                  color: Colors.black,
                                  width: (screenW / 7.0-0.5).ceil().toDouble(),
                                  height: (screenW / 7.0).ceil().toDouble(),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '${_dates[index]['days'][i]['day'].toString()}',
                                    style: TextStyle(color: Colors.white),
                                  ));
                            }),
                          )
                        ],
                      );
                    }),
                  ),
                ),
              )
            ],
          ),
        ));
  }

  // @override
  // Widget build(BuildContext context) {
  //   var screenW = MediaQuery.of(context).size.width;
  //   print(screenW.toString());
  //   return Scaffold(
  //     appBar: AppBar(
  //       title: Text('Calendar22'),
  //       backgroundColor: Color(0xff123456),
  //       elevation: 0,
  //     ),
  //     body: Flex(
  //       direction: Axis.vertical,
  //       children: [
  //         Flex(
  //           direction: Axis.horizontal,
  //           children: [
  //             Expanded(
  //               child: Text(
  //                 '日',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '一',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '二',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '三',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '四',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '五',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //             Expanded(
  //               child: Text(
  //                 '六',
  //                 textAlign: TextAlign.center,
  //               ),
  //               flex: 1,
  //             ),
  //           ],
  //         ),
  //         Container(
  //           height: 600,
  //           color: Colors.black,
  //           child: SingleChildScrollView(
  //             physics: BouncingScrollPhysics(),
  //             child: Flex(
  //               direction: Axis.vertical,
  //               children: List<Widget>.generate(_dates.length, (index) {
  //                 return Flex(
  //                   direction: Axis.vertical,
  //                   crossAxisAlignment: CrossAxisAlignment.start,
  //                   children: [
  //                     Container(
  //                       padding: EdgeInsets.fromLTRB(20, 10, 10, 0),
  //                       child: Text(
  //                         '${_dates[index]['year']}年${_dates[index]['month']}月',
  //                         style: TextStyle(
  //                             fontWeight: FontWeight.bold,
  //                             color: Color(0xffffffff)),
  //                       ),
  //                     ),
  //                     Wrap(
  //                       direction: Axis.horizontal,
  //                       alignment: WrapAlignment.start,
  //                       spacing: 0,
  //                       runSpacing: 0,
  //                       children: List<Widget>.generate(
  //                           _dates[index]['days'].length, (i) {
  //                         if (_dates[index]['days'][i]['single'] == true ||
  //                             _dates[index]['days'][i]['begin'] == true ||
  //                             _dates[index]['days'][i]['middle'] == true ||
  //                             _dates[index]['days'][i]['end'] == true) {
  //                           Radius left = Radius.circular(0);
  //                           Radius right = Radius.circular(0);
  //                           if (_dates[index]['days'][i]['single'] == true ||
  //                               _dates[index]['days'][i]['begin'] == true) {
  //                             left = Radius.circular(screenW / 7.0 / 2.0);
  //                           }
  //                           if (_dates[index]['days'][i]['single'] == true ||
  //                               _dates[index]['days'][i]['end'] == true) {
  //                             right = Radius.circular(screenW / 7.0 / 2.0);
  //                           }

  //                           return ClipRRect(
  //                             borderRadius: BorderRadius.horizontal(
  //                                 left: left,
  //                                 right: right),
  //                             child: Container(
  //                                 color: Colors.orange,
  //                                 width: screenW / 7.0 - 0.01,
  //                                 height: screenW / 7.0,
  //                                 alignment: Alignment.center,
  //                                 child: Text(
  //                                   '${_dates[index]['days'][i]['day'].toString()}',
  //                                   style: TextStyle(color: Colors.white),
  //                                 )),
  //                           );
  //                         }
  //                         return Container(
  //                             color: Colors.black,
  //                             width: screenW / 7.0 - 0.01,
  //                             height: screenW / 7.0,
  //                             alignment: Alignment.center,
  //                             child: Text(
  //                               '${_dates[index]['days'][i]['day'].toString()}',
  //                               style: TextStyle(color: Colors.white),
  //                             ));
  //                       }),
  //                     )
  //                   ],
  //                 );
  //               }),
  //             ),
  //           ),
  //         )
  //       ],
  //     ),
  //   );
  // }
}
